package com.minibobbo.utilities.spe.physicsmodules;

public class SpePhysicsSolidNoGravity extends SpePhysicsModule {

	@Override
	public void updatePhysics(float delta) {

		parent.destination.y += parent.changeVector.cpy().mul(delta).y;
		parent.destination.y += parent.physicsVector.cpy().mul(delta).y;

		parent.destination.x += parent.changeVector.cpy().mul(delta).x;
		parent.destination.x += parent.physicsVector.cpy().mul(delta).x;

		parent.resetTouches();

		checkDown();
		checkUp();
		checkLeft();
		checkRight();
		checkDown();
		checkUp();

	}


	/**
	 * Do we need to make adjustments down?
	 * 
	 * @return True if we moved, otherwise false.
	 */
	public boolean checkDown() {
		// If we aren't trying to move into a new tile, don't check anything.
		if ((int) parent.loc.getBottomSide() <= (int) parent.destination
				.getBottomSide())
			return false;
		// If we are trying to move into a new tile, check the corners to see if
		// movement into the tile is allowed.
		else {
			// Get the tile that the lower left corner is trying to enter.
			int lx = (int) parent.destination.getLL().x;
			int rx = (int) parent.destination.getLR().x;
			int yy = (int) parent.destination.getLL().y;

			// If the tile is solid and the tile above it is open, move up. If
			// the tile is closed and the tile above it is closed, then we have
			// a horizontal collision and do nothing. We will resolve it in the
			// horizontal checks.
			for (int i = lx; i <= rx; i++) {
				if (e.getCurrentMap().getTileContact(i, yy)
						&& !e.getCurrentMap().getTileContact(i, yy + 1)) {
					parent.destination.setBottom((int) parent.destination
							.getBottomSide() + 1);
					parent.changeVector.y = 0;
					parent.physicsVector.y = 0;
					parent.physicsVector.x = 0;
					parent.tD = true;
					return true;
				}
			}
			return false;
		}

	}

	/**
	 * Do we need to make adjustments up?
	 * 
	 * @return True if we moved, otherwise false.
	 */
	public boolean checkUp() {
		if ((int) parent.loc.getTopSide() >= (int) parent.destination
				.getTopSide()) {
			return false;
		} else {
			int lx = (int) parent.destination.getUL().x;
			int rx = (int) parent.destination.getUR().x;
			int yy = (int) parent.destination.getUL().y;
			for (int i = lx; i <= rx; i++) {

				if (e.getCurrentMap().getTileContact(i, yy)
						&& !e.getCurrentMap().getTileContact(i, yy - 1)) {
					parent.destination
							.setTop((int) parent.loc.getTopSide() + .99999f);
					parent.physicsVector.y = 0;
					parent.tU = true;
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * Do we need to make adjustments left?
	 * 
	 * @return True if we moved. Otherwise false;
	 */
	public boolean checkLeft() {
		if ((int) parent.loc.getLeftSide() <= (int) parent.destination
				.getLeftSide())
			return false;
		else {

			int xx = (int) parent.destination.getLL().x;
			int ly = (int) parent.destination.getLL().y;
			int uy = (int) parent.destination.getUL().y;

			for (int i = ly; i <= uy; i++) {

				if (e.getCurrentMap().getTileContact(xx, i)) {
					parent.destination.setLeft((int) parent.loc.getLeftSide());
					parent.changeVector.x = 0;
					parent.tL = true;
					return true;
				}
			}

			return false;
		}

	}

	public boolean checkRight() {
		if ((int) parent.loc.getRightSide() >= (int) parent.destination
				.getRightSide())
			return false;
		else {

			int xx = (int) parent.destination.getLR().x;
			int ly = (int) parent.destination.getLR().y;
			int uy = (int) parent.destination.getUR().y;

			for (int i = ly; i <= uy; i++) {
				if (e.getCurrentMap().getTileContact(xx, i)) {
					parent.destination
							.setRight((int) parent.loc.getRightSide() + .99f);
					parent.changeVector.x = 0;
					parent.tR = true;
					return true;
				}
			}
			return false;
		}

	}

}
